<?php
include_once 'MY/extra_sql.php';
function merger($arr,$data) {
    $array=array('0'=>'0');
    $j=count($data);
    for($i=0;$i<$j;$i++) {
        if(!in_array($data[$i],$arr)) {
            $array[].=$data[$i];
        }
    }
    return $array;
}
function GetID($prefix) {
	//第一步:初始化种子
	//microtime(); 是个数组
	$seedstr =split(" ",microtime(),5);
	$seed =$seedstr[0]*10000;
	//第二步:使用种子初始化随机数发生器
	srand($seed);
	//第三步:生成指定范围内的随机数
	$random =rand(1000,10000);

	$filename = date("Ymd", time()).$random.$prefix;

	return $filename;
}
class CourseController extends Zend_Controller_Action{

    	function init()
	{
               if($_SESSION['user']==""){
                     echo "<script>window.location='../user/login';</script>"; exit;
                }
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "course";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');

		$group_id = $_SESSION['user']['group_id'];
		$group = new Group();
		$group = $group->getgroupByid($group_id);
		$groups = explode(',',$group[0]['models']);
		if(!in_array('course',$groups)){
			  echo "<script>alert('您没有权限访问此模块');history.go('-1');</script>";
		}
	}

	public function indexAction(){
                $course = new Course();
                $courses = $course->getAllCourses('','','','','');
                $this->view->total = count($courses);
                $this->view->top = $course->getTopCourses();
		echo $this->view->render('course/index.html');
	}

//添加课程方法
        public function addAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $operation = new Operation();
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $name=$filter->filter(($this->_request->getPost('name')));
                $course_time=$filter->filter(($this->_request->getPost('course_time')));
                $summary=$this->_request->getPost('summary');
                $user_id=$_SESSION['user']['user_id'];
                $data = array(
                    'name'=>$name,
                    'summary'=>$summary,
                    'user_id'=>$user_id
                );

                $course_id = $course->insert($data);
                if($course_id){
                    $time = time();
                    $data = array(
                        'op_name'=>'add',
                        'model_name'=>'course',
                        'user_id'=>$_SESSION['user']['user_id'],
                        'user_name'=>$_SESSION['user']['user_name'],
                        'name'=>'增加课程',
                        'create_time'=>$time
                    );
                    $query = $operation->insert($data);
                    echo "<script>window.location='detail?course_id=".$course_id."'</script>";
                }else{
                     echo "<script>alert('failure!
')</script>";
                }
            }
            echo $this->view->render('course/add.html');
        }

 //修改课程方法
  public function editAction(){
    $filter=new Zend_Filter_StripTags();
    $course = new Course();
    $teacher = new Teacher();
    $class = new Classes();
    $operation = new Operation();
    $course_id=$filter->filter(($this->_request->getParam('course_id')));

    if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $name=$filter->filter(($this->_request->getPost('name')));
                $summary=$this->_request->getPost('summary');


                $data = array(
                    'name'=>$name,
                    'summary'=>$summary
                );

                $course->updatecourseBycourse_id($data, $course_id);
                $time = time();
                $data = array(
                    'op_name'=>'edit',
                    'model_name'=>'course',
                    'user_id'=>$_SESSION['user']['user_id'],
                    'user_name'=>$_SESSION['user']['user_name'],
                    'name'=>'更新课程',
                    'create_time'=>$time
                );
                $query = $operation->insert($data);
                echo "<script>window.location='detail?course_id=".$course_id."'</script>";

    }else{
        $this->view->course=$course->getcourseBycourse_id($course_id);
    }
    echo $this->view->render('course/edit.html');
}


//课程列表页
       public function listAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $operation = new Operation();
            $db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
            $page=$filter->filter(($this->_request->getParam('page')));
            $search_name=$filter->filter(($this->_request->getParam('search_name')));
            if($page==""){
                $page="1";
            }
            $count = $db->select_courses('','',$search_name);

    //总条数
            $total = count($count);
    //总页数
            $perpage = "10";
            $pages = ceil($total/$perpage);
            $start = ($page-1)*$perpage;
            $end = $perpage;
            $this->view->courses =$db->select_courses($start,$end,$search_name);


            $this->view->page = $page;
            $this->view->pages = $pages;
            $this->view->total = $total;


                $time = time();
                $data = array(
                    'op_name'=>'list',
                    'model_name'=>'course',
                    'user_id'=>$_SESSION['user']['user_id'],
                    'user_name'=>$_SESSION['user']['user_name'],
                    'name'=>'取得所有课程',
                    'create_time'=>$time
                );
                $query = $operation->insert($data);
            echo $this->view->render('course/list.html');
       }

       public function deleteprojectAction(){
        $filter=new Zend_Filter_StripTags();
        $courseproject = new CourseProject();
        $course_id=$filter->filter(($this->_request->getParam('course_id')));
       	$project_id=$filter->filter(($this->_request->getParam('project_id')));
       	$courseproject->delcourseprojectByprojectid($project_id,$course_id);
       	echo "<script>window.location='detail?course_id=".$course_id."'</script>";
       	
       }
       
       public function deletefileAction(){
       	
       	$filter=new Zend_Filter_StripTags();
       	$coursefile = new CourseFile();
       	$course_id=$filter->filter(($this->_request->getParam('course_id')));
       	$file_id=$filter->filter(($this->_request->getParam('file_id')));
       	$coursefile->delcourse_fileByfile_id($file_id);
       	echo "<script>window.location='detail?course_id=".$course_id."'</script>";

      }
       
 //课程详情页
       public function detailAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $class = new Classes();
            $teacher = new Teacher();
            $coursefile = new CourseFile();
            $project = new Project();
            $courseproject = new CourseProject();
            $operation = new Operation();
            $db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
            $page=$filter->filter(($this->_request->getParam('page')));
            $course_id=$filter->filter(($this->_request->getParam('course_id')));
            $course_page=$filter->filter(($this->_request->getParam('course_page')));
            $file_page=$filter->filter(($this->_request->getParam('file_page')));
            
            $coursefiles = $coursefile->getcourse_fileBycourse_id($course_id,'9999','');
            //总条数
			$file_total = count($coursefiles);
           if($file_page==""){
                $file_page="1";
            }
			//总页数
			$perpage = "10";
			$file_pages = ceil($file_total/$perpage);
			$file_start = ($file_page-1)*$perpage;
			$file_end = $perpage;
			 
			$this->view->file_page = $file_page;
			$this->view->file_pages = $file_pages;
			$this->view->file_total = $file_total;
			
			
            $this->view->coursefiles = $coursefile->getcourse_fileBycourse_id($course_id,$file_page,$file_end);
            
            $cases = $courseproject->getcourse_projectBycourse_id($course_id);
             //取出所有的case_id放在一个数组里
                $array = array();
                   for($i=0;$i<count($cases);$i++){
                           $array[].=$cases[$i]['project_id'];
                       }
           if(count($array)!=0){
           	$courseprojects = $project->getAllProjectInArray($array,'9999','');
           	//总条数
			$course_total = count($courseprojects);
	
			//总页数
 
			$course_pages = ceil($course_total/$perpage);
			$course_start = ($course_page-1)*$perpage;
			$course_end = $perpage;
			$this->view->course_page = $course_page;
			$this->view->course_pages = $course_pages;
			$this->view->course_total = $course_total;
			$this->view->courseprojects = $project->getAllProjectInArray($array,$course_page,$course_end);
               
           }
            $this->view->course = $course->getcourseBycourse_id($course_id);

 //comment的页数
            if($page==""){
                $page="1";
            }
            if($course_page==""){
                $course_page="1";
            }
  //查询所有的comment和发表comment的用户名

            $count = $db->select_comment('','',$course_id,'5');

    //总条数
            $total = count($count);

    //总页数
            $perpage = "2";
            $pages = ceil($total/$perpage);
            $start = ($page-1)*$perpage;
            $end = $perpage;
            $this->view->page = $page;
            $this->view->pages = $pages;
            $this->view->total = $total;

            $this->view->comments = $db->select_comment($start,$end,$course_id,'5');

            $time = time();
                $data = array(
                    'op_name'=>'list',
                    'model_name'=>'course',
                    'user_id'=>$_SESSION['user']['user_id'],
                    'user_name'=>$_SESSION['user']['user_name'],
                    'name'=>'取得所有课程',
                    'create_time'=>$time
                );
                $query = $operation->insert($data);
            echo $this->view->render('course/detail.html');
       }

//删除课程
      public function deleteAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $operation = new Operation();
            $course_id=$filter->filter(($this->_request->getParam('course_id')));
            $query = $course->delcourseBycourse_id($course_id);
            if($query){
                $time = time();
                $data = array(
                    'op_name'=>'delete',
                    'model_name'=>'course',
                    'user_id'=>$_SESSION['user']['user_id'],
                    'user_name'=>$_SESSION['user']['user_name'],
                    'name'=>'删除课程',
                    'create_time'=>$time
                );
                $query = $operation->insert($data);
                echo "<script>alert('Success');window.location='list'</script>";
            }else{
                 echo "<script>alert('Failure')</script>";
            }
      }
	   public function hoverAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $search_name=$filter->filter(($this->_request->getParam('search_name')));
            $this->view->courses = $course->getAllcourses('','','',$search_name,'');
            echo $this->view->render('course/hover.html');
       }


	public function fileAction(){
		$filter=new Zend_Filter_StripTags();
		$course = new Course();
                $coursefile = new CourseFile();
              if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $course_id=$filter->filter(($this->_request->getPost('course_id')));
                $summary=$this->_request->getPost('summary');

                $path= "public/files/course/".date("Ym",time())."/";//上传路径
		if(!file_exists($path))
                {
                   mkdir("$path", 0700);
                 }//END IF
                $path= "public/files/course/".date("Ym",time())."/".date("d",time())."/";//上传路径
                if(!file_exists($path))
                {
                  mkdir("$path", 0700);
                 }
                 $name=$_FILES["file_upload"]["name"];
                 $file1=GetID( ".".strtolower(substr($_FILES['file_upload']['name'], (strrpos($_FILES['file_upload']['name'], '.') + 1))));
                 $path = $path.$file1;
                  move_uploaded_file($_FILES["file_upload"]["tmp_name"],$path);


                  $time = time();
                  $array = array(
                      'course_id'=>$course_id,
                      'file_name'=>$name,
                      'summary'=>$summary,
                      'path'=>$path,
                      'create_time'=>$time,
                      'status'=>'0'
                  );
                $file_id = $coursefile->insert($array);
                if($file_id){
                    echo "<script>alert('上传成功');window.location='detail?course_id=".$course_id."'</script>";
                }else{
                    echo "<script>alert('上传失败');window.location='detail?course_id=".$course_id."'</script>";
                }

            }else{

		$course_id=$filter->filter(($this->_request->getParam('course_id')));
                $this->view->course = $course->getcourseBycourse_id($course_id);
            }
		echo $this->view->render('course/file.html');
	}

      //为项目添加案例
	   public function caseaddAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $courseproject = new CourseProject();
                if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                    $course_id=$filter->filter(($this->_request->getPost('course_id')));
                    $project_id=$_POST['case_id'];

                        $count = count($project_id);
                  if($count!=0){
                        for($i=1;$i<=$count;$i++){
                            $data=array(
                              'course_id'=>$course_id,
                              'project_id'=>$project_id[$i-1]
                            );
                            $courseproject->insert($data);
                            unset($data);
                        }
                       echo "<script>alert('添加案例成功');window.location='detail?course_id=".$course_id."'</script>";
                   }
                }else{
                        $this->view->course = $course->getcourseBycourse_id($course_id);
                     //去的所有的项目id，然后去除已经存在的和该项目本身
                        $cases = $courseproject->getcourse_projectBycourse_id($course_id);
                        //取出所有的project_id放在一个数组里
                        $array = array();
                           for($i=0;$i<count($cases);$i++){
                                           $array[].=$cases[$i]['project_id'];
                                       }
                        $projects = $course->getAllCourses('','','','','');
                        $data = array();
                           for($i=0;$i<count($projects);$i++){
                                           $data[].=$projects[$i]['project_id'];
                                       }

                        $pros = merger($array, $data);
                        $this->view->project_cases = $project->getAllProjectInArray($pros);
                        print_r( $this->view->project_cases);
                }
            echo $this->view->render('course/case.html');
       }

   //根据项目id，取出该项目所有案例
      public function caseAction(){
            $filter=new Zend_Filter_StripTags();
            $course = new Course();
            $project = new Project();
            $courseproject = new CourseProject();
            $course_id=$filter->filter(($this->_request->getParam('course_id')));

            $cases = $courseproject->getcourse_projectBycourse_id($course_id);
            //取出所有的case_id放在一个数组里
            $array = array();
               for($i=0;$i<count($cases);$i++){
                               $array[].=$cases[$i]['project_id'];
                           }
            if(count($array)!=0){
               $this->view->project_cases = $project->getAllProjectInArray($array,'9999','');

            }

          //去的所有的项目id，然后去除已经存在的和该项目本身
            $projects = $project->getAllProjects('','');
            $data = array();
                      for($i=0;$i<count($projects);$i++){
                      $data[].=$projects[$i]['project_id'];
                     }
            $pros = merger($array, $data);
            $this->view->projects = $project->getAllProjectInArray($pros,'9999','');
            $this->view->course = $course->getcourseBycourse_id($course_id);
            echo $this->view->render('course/case.html');
      }

//删除案例
public function casedelAction(){
            $filter=new Zend_Filter_StripTags();
            $courseproject = new CourseProject();
            $project_id=$filter->filter(($this->_request->getParam('project_id')));
            $course_id=$filter->filter(($this->_request->getParam('course_id')));
            $query = $courseproject->delcourseprojectByprojectid($project_id, $course_id);
            if($query){
                echo "<script>alert('Success');window.location='case?course_id=".$course_id."'</script>";
            }
    }

}
?>
